package net.easyjava.framework.test.esarch;

import com.alibaba.fastjson.JSONObject;
import net.easyjava.framework.web.elastiesearch.ESearchUtils;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wenxiaoning on 16/6/18.
 */
public class Main {
    public static void main(String[] args) {
        long begin = System.currentTimeMillis();
        SearchResponse searchResponse = ESearchUtils.search("video_1","tweet","song");
        System.out.println("(System.currentTimeMillis()-begin) = " + (System.currentTimeMillis() - begin));

        System.out.println("searchResponse.getTook() = " + searchResponse.getTook());
        System.out.println("searchResponse.isTerminatedEarly() = " + searchResponse.isTerminatedEarly());
        if(searchResponse.getHits().getHits().length>0){
            for(SearchHit searchHit:searchResponse.getHits().getHits()){
                System.out.println("searchHit.getSourceAsString() = " + searchHit.getSourceAsString());
            }
        }
        System.out.println("(System.currentTimeMillis()-begin) = " + (System.currentTimeMillis() - begin));
//        SearchResponse response = ESearchUtils.search("video_1","tweet","武汉,宋冬野");

    }

    public static List<String> getVideoList(){
        Connection conn = null;// DBTools.instance().getConn();
        String sql = "SELECT * FROM VIDEO WHERE id<1000";
        PreparedStatement pstmt;

        List<String> list = new ArrayList<>();
        try {
            pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            System.out.println("rs.getFetchSize() = " + rs.getFetchSize());
            while(rs.next()){
                long id = rs.getLong("id");
                String name = rs.getString("name");
                String tag = rs.getString("tag");
                long showTime = rs.getLong("show_time");
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id",id);
                jsonObject.put("name",name);
                jsonObject.put("tag",tag);
                jsonObject.put("showTime",showTime);
                list.add(jsonObject.toJSONString());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
